﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using System.Collections.Generic;
using DocumentFormat.OpenXml.Spreadsheet;
using ExtremeML.Core;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Spreadsheet
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class AutoFilterWrapper
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing <see cref="WorksheetWrapper"/>.
		/// </summary>
		public WorksheetWrapper Worksheet
		{
			get { return GetWorksheetWrapper(); }
		}

		/// <summary>
		/// Gets the <see cref="CellArea"/> that manages range addressing for the wrapped object.
		/// </summary>
		public CellArea CellArea { get; private set; }

		#endregion
		#region Custom Methods


		#endregion
		#region Overrides

		/// <summary>
		/// Implements custom initialization functionality.
		/// </summary>
		protected override void OnCustomInitializing()
		{
			CellArea = new CellArea();
		}

		/// <summary>
		/// Implements custom population functionality.
		/// </summary>
		protected override void OnCustomDecomposing()
		{
			CellArea = AddressParser.ParseCellArea(Reference);
		}

		/// <summary>
		/// Establishes relationships with other package content objects.
		/// </summary>
		protected override void OnCustomActivating()
		{
			RegisterAddressDependencies();
		}

		/// <summary>
		/// Implements custom persistence functionality.
		/// </summary>
		protected override void OnCustomRecomposing(
			AutoFilter wrapped)
		{
			Reference = CellArea.Range;
		}

		/// <summary>
		/// Implements custom deletion functionality.
		/// </summary>
		protected override void OnCustomRemoved()
		{
			ReleaseAddressDependencies();
		}

		/// <summary>
		/// Returns the set of <see cref="IWrapper"/> instances defined as custom properties.
		/// </summary>
		protected override IEnumerable<IWrapper> GetCustomWrapperMembers()
		{
			yield return CellArea;
		}

		#endregion
		#region Implementation Methods

		/// <summary>
		/// Registers addressable contained objects for notification of address changes.
		/// </summary>
		private void RegisterAddressDependencies()
		{
			Worksheet.WorksheetPart.AddressDependencies.Add(CellArea);

			CellArea.RangeDeleted += HandleRangeDeletion;
		}

		/// <summary>
		/// Releases previously registered contained objects from notification of address changes.
		/// </summary>
		private void ReleaseAddressDependencies()
		{
			if (Worksheet.WorksheetPart.AddressDependencies.Contains(CellArea))
			{
				Worksheet.WorksheetPart.AddressDependencies.Remove(CellArea);
			}

			CellArea.RangeDeleted -= HandleRangeDeletion;
		}

		/// <summary>
		/// Handles the range deletion event of <see cref="CellArea"/>.
		/// </summary>
		private void HandleRangeDeletion(
			object sender,
			EventArgs e)
		{
			Remove();
		}

		/// <summary>
		/// Returns a reference to the containing <see cref="WorksheetWrapper"/>.
		/// </summary>
		private WorksheetWrapper GetWorksheetWrapper()
		{
			if (Parent.GetType().Equals(typeof(WorksheetWrapper)))
			{
				return (WorksheetWrapper)Parent;
			}
			
			if (Parent.GetType().Equals(typeof(TableWrapper)))
			{
				return ((TableWrapper)Parent).WorksheetPart.Worksheet;
			}

			throw new NotImplementedException("Auto filters are currently only supprted in worksheets and tables");
		}

		#endregion
		#region Static Methods


		#endregion
	}
}
